function createList(pageNo = 1, pageSize = 10) {

    let orderID = $('#orderID').val() || null;
    let userID = $('#userUid').val() || null;
    let matchData = {};
    if (!!orderID) matchData.orderNo = orderID;
    if (!!userID) matchData.uid = userID;

    var start = (pageNo - 1) * pageSize;
    apiAdminGetRecord('orderModel', start, pageSize, matchData,
        function (data) {
            setTimeout(() => {
                $('#dataList').bootstrapTable('load', data.msg);
            }, 200)
        });

    // matchData.status = 1;
    // apiAdminGetRecord('orderModel', 0, 0, matchData,
    //     function (data) {
    //         setTimeout(() => {
    //             let total = 0;
    //             for (let i = 0; i < data.msg.recordArr.length; i++) {
    //                 total += data.msg.recordArr[i].count;
    //             }
    //             if (total > 10000) {
    //                 total -= 10000;
    //             }
    //             $('#total').text('总计:  ' + total);
    //         }, 200)
    //     });


}

function deleteUnion(unionID) {

    let permission = localStorage.getItem('permission', '');
    if (permission != -1) {
        $.modal({ body: '权限不足' });
        return;
    }

    Swal.fire({
        title: '确定要删除联盟？',
        text: "联盟删除后无法恢复，请谨慎操作",
        icon: 'question',
        showCancelButton: true,
        confirmButtonColor: '#198754',
        cancelButtonColor: '#d93444',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
    }).then((result) => {
        if (result.isConfirmed) {
            apiDeleteUnion(unionID, function () {
                createList();
                $.modal({ body: '操作成功' });
            });
        }
    });

}

function unionSetMain(unionID, isMain) {
    // let message = '是否确定推荐该联盟';
    // if (isMain == 0) {
    //     message = '是否取消推荐该联盟';
    // }
    // if (!confirm(message)) return;
    apiUnionSetMain(unionID, isMain, function () {
        $.modal({ body: '操作成功' });
        createList(1);
    });
}

$(document).ready(function () {
    // 初始化数据列名
    //数据表格初始化
    $('#dataList').bootstrapTable({
        showRefresh: true,
        striped: true, // 是否显示行间隔色
        pageNumber: 1, // 初始化加载第一页
        pagination: true, // 是否分页
        sidePagination: 'server', // server:服务器端分页|client：前端分页
        pageSize: 10, // 单页记录数
        pageList: [10, 20, 30],
        theadClasses: "thead-light",//这里设置表头样式
        totalField: "totalCount",
        smartDisplay: false,
        buttonsClass: 'light',
        buttonsPrefix: 'btn',
        dataField: "recordArr",
        maintainSelected: true,
        showRefresh: false,// 刷新按钮
        onPageChange: function (number, size) {
            createList(number, size);
        },
        onPostBody: function () {
            //重新启用弹出层,否则formatter格式化函数返回的html字符串上的tooltip提示不生效
            $('[data-bs-toggle="tooltip"]').each(function (i, el) {
                new bootstrap.Tooltip(el)
            })
        },
        onLoadSuccess: function () {
            //重新启用弹出层,否则formatter格式化函数返回的html字符串上的tooltip提示不生效
            $('[data-bs-toggle="tooltip"]').each(function (i, el) {
                new bootstrap.Tooltip(el)
            })
        },
        queryParams: function (params) { // 上传服务器的参数
            var temp = {//如果是在服务器端实现分页，limit、offset这两个参数是必须的
                limit: params.limit, // 每页显示数量
                offset: params.offset, // SQL语句起始索引
            };
            return temp;
        },
        columns: [
            //     {
            //     checkbox: true,
            //     align: "center",
            //     valign: "middle",
            // }, 
            {
                title: 'id',
                field: '_id',
                visible: false
            }, {
                title: '玩家ID',
                width: 200,
                align: "center",
                valign: "middle",
                field: 'uid',
            },
            {
                title: '订单号',
                width: 300,
                align: "center",
                valign: "middle",
                field: 'orderNo',
            }, {
                title: '支付方式',
                width: 150,
                align: "center",
                valign: "middle",
                field: 'payType',
                formatter: function (value) {
                    if (value == 'alipay') {
                        return `<span><i class="bi bi-alipay fs-3 text-primary"></i></span>`
                    } else if (value == 'wxpay') {
                        return `<span><i class="bi bi-wechat fs-3 text-success"></i></span>`
                    }
                    else if (value == 'iospay') {
                        return `<span><i class="bi bi-apple fs-3 text-info"></i></span>`
                    } else {
                        return "未知"
                    }
                }
            }, {
                title: '金额',
                width: 150,
                align: "center",
                valign: "middle",
                field: 'count',
            }, {
                title: '钻石',
                width: 200,
                align: "center",
                valign: "middle",
                field: 'gold',
            }, {
                title: '赠送钻石',
                width: 100,
                align: "center",
                valign: "middle",
                field: 'addGold',
            },
            {
                title: '创建时间',
                width: 200,
                align: "center",
                valign: "middle",
                field: 'status',
                formatter: orderStatus
            },
            {
                title: '创建时间',
                width: 200,
                align: "center",
                valign: "middle",
                field: 'createTime',
                formatter: function (value) {
                    return new Date(value).toLocaleString();
                }
            },
            {
                title: '完成时间',
                width: 200,
                align: "center",
                valign: "middle",
                field: 'endTime',
                formatter: function (value) {
                    if (value) {
                        return new Date(value).toLocaleString();
                    }

                }
            }]
    });
    $('#dataList').bootstrapTable('hideLoading');
    createList(1);
});


function orderStatus(value, row, index) {
    var htm = "";
    if (value == 0) {
        htm = `<span class=" badge text-bg-secondary">未支付</span>`;
    } else if (value == 1) {
        htm = `<span class=" badge text-bg-success">成功</span>`;
    } else if (value == -1) {
        htm = `<span class=" badge text-bg-danger">失败</span>`;
    } else if (value == -2) {
        htm = `<span class=" badge text-bg-danger">已取消</span>`;
    }
    return htm;

}




